import requests,os,shutil
from lxml import etree

if os.path.exists('static/89免费代理'):
    shutil.rmtree('static/89免费代理')
    os.mkdir('static/89免费代理')
else:
    os.mkdir('static/89免费代理')

url = 'https://www.89ip.cn/pvq/index_1.html'


res = requests.get(url)
page = etree.HTML(res.text)
datas = page.xpath('//table/tbody/tr')[1:]

for data in datas:
    ip = data.xpath('./td[1]/text()')[0].strip()
    port = data.xpath('./td[2]/text()')[0].strip()
    location = data.xpath('./td[3]/text()')[0].strip()
    company = data.xpath('./td[4]/text()')[0].strip()
    time = data.xpath('./td[5]/text()')[0].strip()
    print(ip,port,location,company,time,end='')

    safe_location = "".join(c for c in location if c.isalnum() or c in (' ', '-', '_')).rstrip()


    with open(f'static/89免费代理/{safe_location}.csv', 'w', encoding='utf-8') as f:
        f.write(f'{ip},{port},{location},{company},{time}\n')
        # f.write(ip)
        # f.write(port)
        # f.write(location)
        # f.write(company)
        # f.write(time)





